import unittest
import timeit
from alg import Solution
from gen import generate_test_cases


class TestLongestPalindrome(unittest.TestCase):
    def setUp(self):
        self.solution = Solution()

    def test_empty_string(self):
        self.assertEqual(self.solution.longestPalindrome(""), "")

    def test_single_character(self):
        self.assertEqual(self.solution.longestPalindrome("a"), "a")

    def test_two_same_characters(self):
        self.assertEqual(self.solution.longestPalindrome("aa"), "aa")

    def test_two_different_characters(self):
        self.assertEqual(self.solution.longestPalindrome("ab"), "a")

    def test_odd_length_palindrome(self):
        self.assertEqual(self.solution.longestPalindrome("abcba"), "abcba")

    def test_even_length_palindrome(self):
        self.assertEqual(self.solution.longestPalindrome("abccba"), "abccba")

    def test_multiple_palindromes(self):
        self.assertEqual(self.solution.longestPalindrome("babad"), "bab")

    def test_random_cases(self):
        test_cases = generate_test_cases(20)
        for s in test_cases:
            # 测量动态规划算法的时间
            dp_time = timeit.timeit(lambda: self.solution.longestPalindrome(s), number=100)
            # 测量暴力算法的时间
            brute_force_time = timeit.timeit(lambda: self.solution.longestPalindrome_brute_force(s), number=100)

            # 获取动态规划算法和暴力算法得到的最大回文子串
            dp_result = self.solution.longestPalindrome(s)
            brute_force_result = self.solution.longestPalindrome_brute_force(s)

            print(f"Generated String: {s}")
            print(f"Dynamic Programming Result: {dp_result}")
            print(f"Dynamic Programming Time: {dp_time} seconds")
            print(f"Brute Force Result: {brute_force_result}")
            print(f"Brute Force Time: {brute_force_time} seconds")
            print(f"Time Difference: {brute_force_time - dp_time} seconds")
            print()


if __name__ == "__main__":
    unittest.main()